package cn.maihe.elg.operation.centers.fuzhou.handler.service;

import cn.maihe.elg.operation.centers.fuzhou.config.FZMakeInfoConfig;
import cn.maihe.elg.operation.centers.fuzhou.dto.FZBaseRespDTO;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.FZCorrectApplyReqDTO;
import cn.maihe.elg.operation.centers.fuzhou.handler.FZCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.CenterProviderEnum;
import cn.maihe.elg.operation.model.enums.TradeType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeDO;
import cn.maihe.elg.operation.service.business.InstitutionService;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description 保函理赔申请
 * @Author caixh
 * @Date 2022/3/30.
 */
@CenterTradeService(
        provider = CenterProviderEnum.ESB_FUZHOU,
        centerNo = CenterNoEnum.FUZHOU,
        tradeType = TradeType.lstCorrect,
        reqClass = FZCorrectApplyReqDTO.class,
        respClass = FZBaseRespDTO.class
)
@Slf4j
public class FZCorrectApplyService extends FZCenterTradeService<FZCorrectApplyReqDTO, FZBaseRespDTO> {

    @Resource
    ApplyOrderInfoService applyOrderInfoService;
    @Resource
    AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    RefundOrderInfoService refundOrderInfoService;
    @Resource
    ProjectInfoService projectInfoService;
    @Resource
    private InstitutionService institutionService;
    @Resource
    GuaranteeService guaranteeService;

    public FZCorrectApplyService(FZMakeInfoConfig centerInfosConfig) {
        super(centerInfosConfig);
    }


    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FZBaseRespDTO processService(CenterTradeContext<FZCorrectApplyReqDTO, FZBaseRespDTO> context) {

        FZCorrectApplyReqDTO req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();
        String revokeNo = req.getPolicyNo();

        AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByGuaranteeNo(centerId, orgId, revokeNo);
        if (orderInfo == null) {
            return FZBaseRespDTO.error("查无此投保申请记录");
        }

        GuaranteeDO guaranteeDO = guaranteeService.getGuaranteeByGuaranteeNo(orderInfo.getGuaranteeNo());
        if (guaranteeDO == null) {
            return FZBaseRespDTO.error("查无此保函记录");
        }

        if (orderInfo.getStatus() == AcceptOrderStatus.FINISH.getStatus()) {
            return FZBaseRespDTO.error("保函已完成，不能进行批改");
        }
        if (orderInfo.getStatus() == AcceptOrderStatus.CANCEL.getStatus()) {
            return FZBaseRespDTO.error("保函已取消，不能进行批改");
        }
        if (orderInfo.getStatus() != AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()) {
            return FZBaseRespDTO.error("已出函状态才能发起批改");
        }
        //todo:alan 类型转换待处理
        cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeCorrectApplyReqDTO hyMakeCorrectApplyReqDTO
                = new cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeCorrectApplyReqDTO();
        try {
            PropertyUtils.copyProperties(hyMakeCorrectApplyReqDTO,req);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        boolean result = acceptOrderInfoService.correctGuarantee(orderInfo,guaranteeDO,hyMakeCorrectApplyReqDTO);
        if (!result){
            return FZBaseRespDTO.error("重新出函失败");
        }
        return FZBaseRespDTO.success("批改已受理,请耐心等待审核结果通知");
    }
}
